<template><div><h1 id="math" tabindex="-1"><a class="header-anchor" href="#math" aria-hidden="true">#</a> Math</h1>
<h2 id="_1-只保留整数部分-丢弃小数部分" tabindex="-1"><a class="header-anchor" href="#_1-只保留整数部分-丢弃小数部分" aria-hidden="true">#</a> <strong>1.只保留整数部分（丢弃小数部分）</strong></h2>
<div class="language-javascript line-numbers-mode" data-ext="js"><pre v-pre class="language-javascript"><code><span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token number">5.1234</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 5</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><h2 id="_2-向下取整-该数值的最大整数" tabindex="-1"><a class="header-anchor" href="#_2-向下取整-该数值的最大整数" aria-hidden="true">#</a> <strong>2.向下取整（&lt;= 该数值的最大整数）</strong></h2>
<div class="language-javascript line-numbers-mode" data-ext="js"><pre v-pre class="language-javascript"><code>Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span><span class="token number">5.55</span><span class="token punctuation">)</span> <span class="token comment">//向下取整 结果为5</span>
Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span><span class="token number">5.99</span><span class="token punctuation">)</span> <span class="token comment">//向下取整 结果为5</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="_3-向上取整-有小数-整数就-1" tabindex="-1"><a class="header-anchor" href="#_3-向上取整-有小数-整数就-1" aria-hidden="true">#</a> <strong>3.向上取整（有小数，整数就+1）</strong></h2>
<div class="language-javascript line-numbers-mode" data-ext="js"><pre v-pre class="language-javascript"><code>Math<span class="token punctuation">.</span><span class="token function">ceil</span><span class="token punctuation">(</span><span class="token number">5.21</span><span class="token punctuation">)</span> <span class="token comment">//向上取整，结果为6</span>
Math<span class="token punctuation">.</span><span class="token function">ceil</span><span class="token punctuation">(</span><span class="token number">5.88</span><span class="token punctuation">)</span> <span class="token comment">//向上取整，结果为6</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="_4-四舍五入-小数部分" tabindex="-1"><a class="header-anchor" href="#_4-四舍五入-小数部分" aria-hidden="true">#</a> <strong>4.四舍五入（小数部分）</strong></h2>
<div class="language-javascript line-numbers-mode" data-ext="js"><pre v-pre class="language-javascript"><code>Math<span class="token punctuation">.</span><span class="token function">round</span><span class="token punctuation">(</span><span class="token number">5.78</span><span class="token punctuation">)</span> <span class="token comment">//四舍五入 结果为6</span>
Math<span class="token punctuation">.</span><span class="token function">round</span><span class="token punctuation">(</span><span class="token number">5.33</span><span class="token punctuation">)</span> <span class="token comment">//结果为5</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="_5-绝对值" tabindex="-1"><a class="header-anchor" href="#_5-绝对值" aria-hidden="true">#</a> <strong>5.绝对值</strong></h2>
<div class="language-javascript line-numbers-mode" data-ext="js"><pre v-pre class="language-javascript"><code>Math<span class="token punctuation">.</span><span class="token function">abs</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 1</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><h2 id="_6-返回两者中的较大值" tabindex="-1"><a class="header-anchor" href="#_6-返回两者中的较大值" aria-hidden="true">#</a> <strong>6.返回两者中的较大值</strong></h2>
<div class="language-javascript line-numbers-mode" data-ext="js"><pre v-pre class="language-javascript"><code>Math<span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 2</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><h2 id="_7-返回两者中的较小值" tabindex="-1"><a class="header-anchor" href="#_7-返回两者中的较小值" aria-hidden="true">#</a> <strong>7.返回两者中的较小值</strong></h2>
<div class="language-javascript line-numbers-mode" data-ext="js"><pre v-pre class="language-javascript"><code>Math<span class="token punctuation">.</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 1</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><h2 id="_8-随机数-0-1" tabindex="-1"><a class="header-anchor" href="#_8-随机数-0-1" aria-hidden="true">#</a> <strong>8.随机数（0-1）</strong></h2>
<p><strong><code v-pre>Math.random()</code></strong> 函数返回一个浮点数,  伪随机数在范围从<strong>0到</strong>小于<strong>1</strong>，也就是说，从0（包括0）往上，但是不包括1（排除1），然后您可以缩放到所需的范围。实现将初始种子选择到随机数生成算法;它不能被用户选择或重置。</p>
<div class="language-javascript line-numbers-mode" data-ext="js"><pre v-pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">getRandomInt</span><span class="token punctuation">(</span><span class="token parameter">max</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> max<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">getRandomInt</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// expected output: 0, 1 or 2</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">getRandomInt</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// expected output: 0</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// expected output: a number from 0 to &lt;1</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div></div></template>


